
function getCookie(name) {

	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}
function setFormValidate($form, fsuccess) {
	var $tooltip = $('<div id="tooltip">提示信息！</div>');
	$tooltip.appendTo(document.body);
	$form.validator({
		onValid : function() {
			$tooltip.hide()
		},
		submit : function() {
			var formValidity = this.isFormValid();
			$.when(formValidity).then(function() {
				if (formValidity == true)

					sumitImageFile(fsuccess);
			}, function() {
			});
		}
	});
	$form.on('focusin focusout', '.am-form-error input', function(e) {
		if (e.type === 'focusin') {
			var $this = $(this);
			var offset = $this.offset();
			var msg = $this.data('foolishMsg')
					|| $form.validator("getValidationMessage", $this
							.data("validity"));

			$tooltip.text(msg).show().css({
				left : offset.left + 10,
				top : offset.top + $(this).outerHeight() + 10
			});
		} else {
			$tooltip.hide();

		}
	});
}

function myAjax(requesturl, data, fsuccess, method) {
	if (typeof (method) == 'undefined') {
		method = 'POST';
	}
	$.ajax({
		url : requesturl, // 请求的url地址
		dataType : "json",
		async : true,
		data : data,
		type : method,

		beforeSend : function() {
		},
		success : fsuccess,
		complete : function() {
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			if (errorThrown == 'Forbidden') {
				window.location.href = domain + '/Login.jsp';
				myAlert('提示', '登录失效，请重新登陆！！！');
			} else
				myAlert('提示', '服务器错误，请稍后再试！！！');
		}
	});
}

function myPost(url, params) {
	var temp = document.createElement("form");
	temp.action = url;
	temp.method = "post";
	temp.style.display = "none";
	for ( var x in params) {
		var opt = document.createElement("textarea");
		opt.name = x;
		opt.value = params[x];
		temp.appendChild(opt);
	}
	document.body.appendChild(temp);
	temp.submit();
	return temp;
}

function drawDiffStatusContent($pagecontainer, data) {
	var temp = '';
	$
			.each(
					data,
					function(i, item) {
						var index = item.id;
						temp = '<td><div class="am-btn-toolbar"><div class="am-btn-group am-btn-group-xs">'
								+ ' <button id=look'
								+ i
								+ ' class=" look am-btn am-btn-default am-btn-xs am-hide-sm-only"><span class="am-icon-pencil-square-o"></span> 查看</button>'
								+ '  <button id=del'
								+ i
								+ ' class="del am-btn am-btn-default am-btn-xs am-hide-sm-only"><span class="am-icon-copy"></span> 删除</button>'

								+ ' </div> </div></td>'

						$pagecontainer.append(' <tr class="device" id=device'
								+ i + '><td><input type="checkbox" id=check'
								+ i + ' class=check value=' + i + '></td><td>'
								+ (i + 1) + '</td><td>' + item.devicename
								+ '</td><td>' + item.devicecode

								+ '</td><td>' + item.holder + '</td> <td>'
								+ item.createtime + '</td>' + temp + '</tr>');

					});

}

function getRootPath() {
	var curWwwPath = window.document.location.href;
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	var localhostPaht = curWwwPath.substring(0, pos);
	var projectName = pathName
			.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (localhostPaht + projectName);
}

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}
var modalid = 0;
function myAlert(texthead, textbody, confirm) {
	modalid++;
	var id = modalid;
	var ops = confirm;
	var html = '<div class="am-modal am-modal-alert" tabindex="-1" id='
			+ id
			+ '><div class="am-modal-dialog">'
			+ '<div class="am-modal-hd" id=alerthd'
			+ id
			+ '></div><div class="am-modal-bd" id=alertbd'
			+ id
			+ '></div>'
			+ '<div class="am-modal-footer"><span class="am-modal-btn" data-am-modal-confirm>确定</span></div></div></div>';

	$('body').append(html);
	$('#alerthd' + id).text(texthead);
	$('#alertbd' + id).text(textbody);

	$('#' + id).modal({
		relatedTarget : this,
		onConfirm : function(e) {
			if (typeof (ops) != 'undefined')
				ops();

		}
	});
	return $('#' + id);
}
function myConfirm(texthead, textbody, confirmfunction, cancelfunction) {
	modalid++;
	var id = modalid;
	var ops = confirmfunction;
	var ops1 = cancelfunction;
	var html = '<div class="am-modal am-modal-alert" tabindex="-1" id='
			+ id
			+ '><div class="am-modal-dialog">'
			+ '<div class="am-modal-hd" id=confirmhd'
			+ id
			+ '></div><div class="am-modal-bd" id=confirmbd'
			+ id
			+ '></div>'
			+ '<div class="am-modal-footer"> <span class="am-modal-btn" data-am-modal-cancel>取消</span><span class="am-modal-btn" data-am-modal-confirm>确定</span></div></div></div>';

	$('body').append(html);
	$confirm = $('#' + id);
	$('#confirmhd' + id).text(texthead);
	$('#confirmbd' + id).text(textbody);
	var $confirmBtn = $confirm.find('[data-am-modal-confirm]');
	var $cancelBtn = $confirm.find('[data-am-modal-cancel]');
	$confirmBtn.off('click.confirm.modal.amui').on('click', function(e) {
		if (typeof (ops) != 'undefined')
			ops();

	});
	$cancelBtn.off('click.cancel.modal.amui').on('click', function(e) {
		if (typeof (ops1) != 'undefined')
			ops1();

	});
	return $confirm;

}
function myHint(texthead, textbody) {
	modalid++;
	var id = modalid;
	var html = '<div class="am-modal am-modal-no-btn" tabindex="-1" id=' + id
			+ '><div class="am-modal-dialog">'
			+ '<div class="am-modal-hd" id=Hinthd' + id
			+ '></div><div class="am-modal-bd" id=Hintbd' + id + '></div>'
			+ '</div></div>';

	$('body').append(html);
	$('#Hinthd' + id).text(texthead);
	$('#Hintbd' + id).append(textbody);
	// $('#'+id).modal('open');
	return $('#' + id);
}
function sumitImageFile(fsuccess, url, formData, type) {
	if (typeof (formData) == 'undefined') {
		var form = document.forms[0];
		var formData = new FormData(form);
	}
	if (typeof (type) == 'undefined')
		type = "POST";
	if (typeof (url) == 'undefined')
		url = form.action;
	$.ajax({
		url : url,
		type : type,
		data : formData,
		dataType : "json",
		processData : false,
		contentType : false,

		success : fsuccess,
		error : function(XMLHttpRequest, textStatus, errorThrown) {

			if (errorThrown == 'Forbidden') {
				window.location.href = domain + '/Login.jsp';
				myAlert('提示', '登录失效，请重新登陆！！！');
			} else
				myAlert('提示', '服务器错误，请稍后再试！！！');
		},
		xhr : function() {
			var xhr = new XMLHttpRequest();
			xhr.upload.addEventListener("progress", function(evt) {
				if (evt.lengthComputable) {
					var percentComplete = Math.round(evt.loaded * 100
							/ evt.total);
					console.log("正在提交." + percentComplete.toString() + '%');
				}
			}, false);

			return xhr;
		}

	});
}
function openPostWindow(url, params)  
{  

var temp = document.createElement("form");
	temp.action = url;
	temp.method = "post";
	temp.style.display = "none";
	for ( var x in params) {
		var opt = document.createElement("textarea");
		opt.name = x;
		opt.value = params[x];
		temp.appendChild(opt);
	}
	  $(temp).attr("target","detail");

	document.body.appendChild(temp);
	   window.open("about:blank","detail","");//newWin 是上面form的target

	temp.submit();
	document.body.removeChild(temp);
 
 
   

}

